/**
 * 
 */
var httpRequest = null;
function removeIngredient(id) {

	// erstellen des requests
	httpRequest = null;
	try {
		httpRequest = new XMLHttpRequest(
				'edit_cocktail_remove_ingredient.xml?id=' + id,
				handleRemoveIngredient);
	} catch (ms) {
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (nonms) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				httpRequest = null;
			}
		}
	}

	if (httpRequest == null) {
		alert("Ajax-Element konnte nicht erzeugt werden.");
		return;
	}
	httpRequest.onreadystatechange = handleRemoveIngredient;
	httpRequest.open("GET", 'edit_cocktail_remove_ingredient.xml?id=' + id,
			true);
	httpRequest.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	httpRequest.send(null);
}


function addIngredient(id, amount) {
	select = document.getElementById('selectable_ingredients');
	id = select.options[select.selectedIndex]['id'];
	amount = document.getElementById('amount').value;
	// erstellen des requests
	 httpRequest = null;

	try {
		httpRequest = new XMLHttpRequest();
	} catch (ms) {
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (nonms) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				httpRequest = null;
			}
		}
	}

	if (httpRequest == null) {
		alert("Ajax-Element konnte nicht erzeugt werden.");
		return;
	}
	httpRequest.onreadystatechange = handleAddIngredient;
	httpRequest.open("GET", 'edit_cocktail_add_ingredient.xml?id=' + id
			+ '&amount=' + amount, true);
	httpRequest.send(null);
}

function handleAddIngredient() {
	switch (httpRequest.readyState) {
	case 4:
		if (httpRequest.status != 200) {
			alert("Fehler:" + httpRequest.status);
		} else {
			ingredients_field = document.getElementById('ingredients');
			xml = httpRequest.responseXML;
			ingredients_field.innerHTML = ingredients_field.innerHTML
					+ '<p id="ingr_'
					+ xml.getElementsByTagName("id")[0].childNodes[0].nodeValue
					+ '"><span>'
					+ xml.getElementsByTagName("name")[0].childNodes[0].nodeValue
					+ '</span><span> <button type="button" onclick="removeIngredient(\''
					+ xml.getElementsByTagName("id")[0].childNodes[0].nodeValue
					+ '\');">entfernen</button</span></p>';
		}
		break;

	default:
		return false;
		break;
	}
}

function handleRemoveIngredient() {
	switch (httpRequest.readyState) {
	case 4:
		if (httpRequest.status != 200) {
			alert("Fehler:" + httpRequest.status);
		} else {
			ingredients_field = document.getElementById('ingredients');
			xml = httpRequest.responseXML;
			remove_ingredient = document
					.getElementById('ingr_'
							+ xml.getElementsByTagName("id")[0].childNodes[0].nodeValue);
			ingredients_field.removeChild(remove_ingredient);
		}
		break;

	default:
		return false;
		break;
	}
}